package com.leased.handler;

import com.leased.enums.UserRoleEnum;
import com.leased.exception.DatabaseException;
import com.leased.exception.YCException;
import com.leased.pojo.AdminInfo;
import com.leased.request.BaseQuery;
import com.leased.request.admin.AdminInfoRequest;
import com.leased.response.AdminInfoResponse;
import com.leased.service.AdminInfoService;
import com.leased.utils.DateTimeUtil;
import com.leased.utils.Page;
import com.leased.utils.Validator;
import com.leased.utils.YCSystemStatusEnum;
import org.omg.PortableInterceptor.INACTIVE;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by shencx on 2017/7/27.
 */
@Service
public class AdminInfoHandler {

    Logger LOG = LoggerFactory.getLogger(AdminInfoHandler.class);

    @Resource
    private AdminInfoService adminInfoService;

    /**
     * 管理员登录
     * @param adminInfo
     * @return
     * @throws YCException
     */
    public AdminInfo adminLogin(AdminInfo adminInfo) throws YCException {
        /** 参数校验 */
        Validator.isEmpty(adminInfo, YCSystemStatusEnum.PARAM_EMPTY);
        Validator.isEmpty(adminInfo.getUserName(),"用户名不能为空");
        Validator.isEmpty(adminInfo.getPassword(),"密码不能为空");
        try {
            AdminInfo adminInfo1 = adminInfoService.fetchAdminInfoLogin(adminInfo);
            return adminInfo1;
        } catch (DatabaseException e) {
            LOG.error("adminLogin exception",adminInfo);
            throw new YCException(YCSystemStatusEnum.INVOKE_API_RETURN_EXCEPTION.getCode(), YCSystemStatusEnum.INVOKE_API_RETURN_EXCEPTION.getDesc());
        }
    }

    /**
     * 添加管理员
     * @param adminInfoRequest
     * @throws YCException
     */
    public void addAdminInfo(AdminInfoRequest adminInfoRequest) throws YCException {
        /** 参数校验 */
        Validator.isEmpty(adminInfoRequest,YCSystemStatusEnum.PARAM_EMPTY);
        Validator.isEmpty(adminInfoRequest.getUserName(),"用户名不能为空");
        Validator.isEmpty(adminInfoRequest.getPassword(),"密码不能为空");
        Validator.isEmpty(adminInfoRequest.getRoleValue(),"角色值不能为空");
        AdminInfo adminInfo = new AdminInfo();
        adminInfo.setId(adminInfoRequest.getId());
        adminInfo.setUserName(adminInfoRequest.getUserName());
        adminInfo.setPassword(adminInfoRequest.getPassword());
        adminInfo.setRoleValue(UserRoleEnum.create(adminInfoRequest.getRoleValue()));
        try {
            if (adminInfo.getId() == null) {
                adminInfoService.addAdminInfo(adminInfo);
            }else {
                adminInfoService.updateAdminInfo(adminInfo);
            }
        } catch (DatabaseException e) {
            LOG.error("addAdminInfo exception",adminInfoRequest);
            throw new YCException(YCSystemStatusEnum.INVOKE_API_RETURN_EXCEPTION.getCode(), YCSystemStatusEnum.INVOKE_API_RETURN_EXCEPTION.getDesc());
        }
    }

    /**
     * 获取管理员列表
     * @param baseQuery
     * @return
     * @throws YCException
     */
    public Page<AdminInfoResponse> getAdminInfoPage(BaseQuery baseQuery) throws YCException {
        Page<AdminInfo> adminInfoPage = null;
        List<AdminInfoResponse> list = new ArrayList<>();
        try {
            adminInfoPage = adminInfoService.fetchAdminInfoList(baseQuery);
            if (adminInfoPage != null){
                for (AdminInfo adminInfo : adminInfoPage.getResult()){
                    list.add(adminConvert(adminInfo));
                }
            }
            Page<AdminInfoResponse> page = new Page<>();
            page.setPageindex(adminInfoPage.getPageindex());
            page.setPagesize(adminInfoPage.getPagesize());
            page.setTotal(adminInfoPage.getTotal());
            page.setResult(list);
            return page;
        }  catch (DatabaseException e) {
            LOG.error("getAdminInfoPage exception",baseQuery);
            throw new YCException(YCSystemStatusEnum.INVOKE_API_RETURN_EXCEPTION.getCode(), YCSystemStatusEnum.INVOKE_API_RETURN_EXCEPTION.getDesc());
        }
    }

    /**
     * 启用、禁用
     * @param id
     * @param status
     * @throws YCException
     */
    public void modifyStatus(Integer id,Integer status) throws YCException {
        /** 参数校验 */
        Validator.isEmpty(id,"主键ID不能为空");
        Validator.isEmpty(status,"状态值不能为空");
        AdminInfo adminInfo = new AdminInfo();
        adminInfo.setId(id);
        adminInfo.setIsUsed(status);
        try {
            adminInfoService.updateAdminInfo(adminInfo);
        }  catch (DatabaseException e) {
            LOG.error("modifyStatus exception",id,status);
            throw new YCException(YCSystemStatusEnum.INVOKE_API_RETURN_EXCEPTION.getCode(), YCSystemStatusEnum.INVOKE_API_RETURN_EXCEPTION.getDesc());
        }
    }

    /**
     * 获取详情
     * @param id
     * @return
     * @throws YCException
     */
    public AdminInfoResponse getAdminInfoById(Integer id) throws YCException {
        /** 参数校验 */
        Validator.isEmpty(id,"主键ID不能为空");
        AdminInfoResponse adminInfoResponse = null;
        try {
            AdminInfo adminInfo = adminInfoService.fetchAdminInfoById(id);
            adminInfoResponse =  adminConvert(adminInfo);
            return adminInfoResponse;
        }catch (DatabaseException e) {
            LOG.error("getAdminInfoById exception",id);
            throw new YCException(YCSystemStatusEnum.INVOKE_API_RETURN_EXCEPTION.getCode(), YCSystemStatusEnum.INVOKE_API_RETURN_EXCEPTION.getDesc());
        }
    }

    /**
     * 对象转换
     * @param adminInfo
     * @return
     */
    public static AdminInfoResponse adminConvert(AdminInfo adminInfo){
        AdminInfoResponse adminInfoResponse = new AdminInfoResponse();
        adminInfoResponse.setId(adminInfo.getId());
        adminInfoResponse.setUserName(adminInfo.getUserName());
        adminInfoResponse.setPassword(adminInfo.getPassword());
        adminInfoResponse.setBeUsed(adminInfo.getIsUsed());
        adminInfoResponse.setRoleValue(adminInfo.getRoleValue().getValue());
        adminInfoResponse.setCreateTime(DateTimeUtil.formatDateTime(adminInfo.getCreateTime()));
        return adminInfoResponse;
    }
}
